/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookingsystem;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class Client {
    public static void main(String[] args) {
        Socket client = null;
        try {
            client = new Socket();
            SocketAddress sockaddr = new InetSocketAddress("192.168.1.6", 7777);
            client.connect(sockaddr, 5000);

            ClientThread ct;
            ct = new ClientThread(client);
            Thread t = new Thread(ct);
            t.start();
            while(true){
                t.sleep(4000);
            }
        } 
        catch (SocketTimeoutException ste){
            System.err.println("Server not available... Switching to backup Database...");
            try {
                client = new Socket();
                SocketAddress sockaddr = new InetSocketAddress("127.0.0.1", 8888);
                client.connect(sockaddr, 5000);

                ClientThread ct;
                ct = new ClientThread(client);
                Thread t = new Thread(ct);
                t.start();
                while(true){
                    t.sleep(4000);
                }
            }
            catch (IOException e) {
                //System.out.println(e);
                e.printStackTrace();
            }
            catch (InterruptedException ie){
                System.out.println(ie);
            }
        }
        catch (ConnectException e) {
            JOptionPane.showMessageDialog(null, "Cannot connect to the server", "Connection Error", 0);
        }
        catch (IOException e) {
            //System.out.println(e);
            e.printStackTrace();
        } catch (InterruptedException ie){
                System.out.println(ie);
        }
    }
    
}
